import {
  FieldInterface,
  TableInterface
} from '@/components/SqlEditor/TableType'
import { Field, Model } from '@/views/moduleDetail/moduleDetailType'
import { defineStore } from 'pinia'

export const useModuleStore = defineStore('module', {
  state: () => ({
    currentModule: {} as Model,
    currentModuleTable: [] as TableInterface[]
  }),
  actions: {
    setCurrentModule (moduleData: Model) {
      this.currentModule = moduleData
      this.setCurrentModuleTable()
    },
    /**
     * 设置当前模块表格
     */
    setCurrentModuleTable () {
      let moduleTable = this.currentModule.table
      let moduleField = this.currentModule.field
      this.currentModuleTable = [
        {
          label: `${moduleTable.name}${
            moduleTable.comment ? '(' + moduleTable.comment + ')' : ''
          }`,
          value: moduleTable.name,
          fields: moduleField.map((item: Field) => {
            return {
              label: `${item.name}${
                item.comment ? '(' + item.comment + ')' : ''
              }`,
              value: item.name,
              aliasName: item.aliasName
            } as FieldInterface
          })
        }
      ]
    }
  }
})
